package com.pospi.xstore.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lany.sp.SPHelper;
import com.pospi.xstore.database.DB;
import com.pospi.xstore.dto.OrderDto;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.GetData;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Qiyan on 2016/5/25.
 */
public class OrderDao {

    private DB OrderDB;
    private String tablename = "orderinfo";

    private OrderDto dto;

    private Context context;

    public OrderDao(Context context) {
        OrderDB = new DB(context);//实例化数据库
        this.context = context;
    }

    /***
     * 给数据库里面添加数据
     */
    public void addOrder(OrderDto orderDto) {
        SQLiteDatabase db = OrderDB.getWritableDatabase();
        db.execSQL("insert into orderinfo(maxNo,orderType,payway,shop_id,time,ss_money,ys_money," +
                "zl_money,order_info,cashiername,cashiernumber,detailTime," +
                "checkoutTime,hasReturnGoods,out_trade_no,payReturn," +
                "ifFinishOrder,searchTime,tableNumber,tableId,vipNumber)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                new Object[]{orderDto.getMaxNo(), orderDto.getOrderType(),
                        orderDto.getPayway(), orderDto.getShop_id(),
                        orderDto.getTime(), orderDto.getSs_money(),
                        orderDto.getYs_money(), orderDto.getZl_money(),
                        orderDto.getOrder_info(), orderDto.getCashiername(),
                        orderDto.getCashiernumber(), orderDto.getDetailTime(),
                        orderDto.getCheckoutTime(), orderDto.getHasReturnGoods(),
                        orderDto.getOut_trade_no(), orderDto.getPayReturn(),
                        orderDto.getIfFinishOrder(), orderDto.getSearchTime(),
                        orderDto.getmTableNumber(),orderDto.getmTableId(),
                        orderDto.getVipNumber()});

        /**
         * 给服务器上传一条数据
         */
        db.close();
        Log.i("添加一条数据", orderDto.getPayway() + orderDto.getShop_id() + orderDto.getTime() + orderDto.getSs_money() + orderDto.getYs_money() + orderDto.getZl_money() + orderDto.getOrder_info());

    }

    public boolean updateOrderInfo(String maxNo) {
        SQLiteDatabase db = OrderDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("hasReturnGoods", Constant.hasReturnGoods_Yes);
        if ((db.update(tablename, values, "maxNo=?", new String[]{String.valueOf(maxNo)})) > 0) {
            return true;
        }
        db.close();
        return false;
    }

    /**
     * 更改挂单的状态
     *
     * @param maxNo
     * @return
     */
    public boolean deleteRestOrderInfo(String maxNo) {
        SQLiteDatabase db = OrderDB.getWritableDatabase();
        if (db.delete(tablename, "maxNo=?", new String[]{String.valueOf(maxNo)}) > 0) {
            return true;
        }
        return false;
    }


    /**
     * 根据时间来查询当天的订单的信息
     * ，只用于查询某一天的订单的信息
     *
     * @param time 当天的日期
     * @return 返回一个当天的所有信息的列表
     */
    public List<OrderDto> selectGoods(String time) {
        String shopId = context.getSharedPreferences("StoreMessage", context.MODE_PRIVATE).getString("Id", "");

        SQLiteDatabase db = OrderDB.getReadableDatabase();

        List<OrderDto> dtosss = new ArrayList<>();
        Cursor cursor = db.query(tablename, null, "time=? and shop_id=? and ifFinishOrder=?", new String[]{time, shopId, String.valueOf(Constant.YES)}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(cursor.getString(cursor.getColumnIndex("maxNo")));
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setIfFinishOrder(cursor.getInt(cursor.getColumnIndex("ifFinishOrder")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));

            dtosss.add(dto);
        }
        cursor.close();
        db.close();
        return dtosss;
    }

    /**
     * 查询最大的小票号
     *
     * @param time 当天的日期
     * @return 返回一个当天的所有信息的列表
     */
    public List<OrderDto> findMaxNO(String time) {
        String shopId = SPHelper.getInstance().getString(Constant.STORE_ID);

        SQLiteDatabase db = OrderDB.getReadableDatabase();

        List<OrderDto> dtosss = new ArrayList<>();
        Cursor cursor = db.query(tablename, null, "time=? and shop_id=?", new String[]{time, shopId}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(cursor.getString(cursor.getColumnIndex("maxNo")));
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));
            dtosss.add(dto);
        }
        cursor.close();
        db.close();
        return dtosss;
    }

    /**
     * 查询当天的所有的挂单的信息
     */
    public List<OrderDto> selectRestOrder() {
        String shopId = SPHelper.getInstance().getString(Constant.STORE_ID);
        String time = GetData.getYYMMDDTime();
        SQLiteDatabase db = OrderDB.getReadableDatabase();

        List<OrderDto> dtosss = new ArrayList<>();
        Cursor cursor = db.query(tablename, null, "time=? and shop_id=? and ifFinishOrder=?", new String[]{time, shopId, String.valueOf(Constant.NO)}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(cursor.getString(cursor.getColumnIndex("maxNo")));
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setIfFinishOrder(cursor.getInt(cursor.getColumnIndex("ifFinishOrder")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));
            dtosss.add(dto);
        }
        cursor.close();
        db.close();
        return dtosss;
    }
    /**
     * 查询当天的信息
     */
    public List<OrderDto> selectTodayOrder(String date) {
        String shopId = SPHelper.getInstance().getString(Constant.STORE_ID);
        String time = date;
        SQLiteDatabase db = OrderDB.getReadableDatabase();

        List<OrderDto> dtosss = new ArrayList<>();
        Cursor cursor = db.query(tablename, null, "time=? and shop_id=? ", new String[]{time, shopId}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(cursor.getString(cursor.getColumnIndex("maxNo")));
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setIfFinishOrder(cursor.getInt(cursor.getColumnIndex("ifFinishOrder")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));
            dtosss.add(dto);
        }
        cursor.close();
        db.close();
        return dtosss;
    }

    /**
     * 根据订单号来进行订单查询
     *
     * @param maxNo
     * @return
     */
    public OrderDto selectOrderByNo(String maxNo) {
        String shopId = SPHelper.getInstance().getString(Constant.STORE_ID);

        SQLiteDatabase db = OrderDB.getReadableDatabase();

        Cursor cursor = db.query(tablename, null, "maxNo=? and shop_id=? and ifFinishOrder=?", new String[]{String.valueOf(maxNo), shopId, String.valueOf(Constant.YES)}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(maxNo);
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setIfFinishOrder(cursor.getInt(cursor.getColumnIndex("ifFinishOrder")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));
        }
        cursor.close();
        db.close();
        return dto;
    }


    /**
     * 知道起始的时间，查询某一个时间段的订单的信息
     *
     * @param from
     * @param to
     * @return
     */
    public List<OrderDto> selectGoodsBetween(int from, int to) {
        String shopId = SPHelper.getInstance().getString(Constant.STORE_ID);

        SQLiteDatabase db = OrderDB.getReadableDatabase();

        List<OrderDto> dtosss = new ArrayList<>();
        Cursor cursor = db.query(tablename, null, "searchTime>=? and searchTime<=? and shop_id=? and ifFinishOrder=?", new String[]{String.valueOf(from), String.valueOf(to), shopId, String.valueOf(Constant.YES)}, null, null, null);
        while (cursor.moveToNext()) {
            dto = new OrderDto();
            //遍历Cursor对象，取出数据并打印
            dto.setMaxNo(cursor.getString(cursor.getColumnIndex("maxNo")));
            dto.setOrderType(cursor.getInt(cursor.getColumnIndex("orderType")));
            dto.setPayway(cursor.getString(cursor.getColumnIndex("payway")));
            dto.setSs_money(cursor.getDouble(cursor.getColumnIndex("ss_money")));
            dto.setYs_money(cursor.getDouble(cursor.getColumnIndex("ys_money")));
            dto.setZl_money(cursor.getDouble(cursor.getColumnIndex("zl_money")));
            dto.setOrder_info(cursor.getString(cursor.getColumnIndex("order_info")));
            dto.setCashiername(cursor.getString(cursor.getColumnIndex("cashiername")));
            dto.setCashiernumber(cursor.getString(cursor.getColumnIndex("cashiernumber")));
            dto.setIfFinishOrder(cursor.getInt(cursor.getColumnIndex("ifFinishOrder")));
            dto.setDetailTime(cursor.getString(cursor.getColumnIndex("detailTime")));
            dto.setCheckoutTime(cursor.getString(cursor.getColumnIndex("checkoutTime")));
            dto.setHasReturnGoods(cursor.getInt(cursor.getColumnIndex("hasReturnGoods")));
            dto.setOut_trade_no(cursor.getString(cursor.getColumnIndex("out_trade_no")));
            dto.setPayReturn(cursor.getString(cursor.getColumnIndex("payReturn")));
            dto.setmTableNumber(cursor.getString(cursor.getColumnIndex("tableNumber")));
            dto.setmTableId(cursor.getString(cursor.getColumnIndex("tableId")));
            dto.setVipNumber(cursor.getString(cursor.getColumnIndex("vipNumber")));
            dtosss.add(dto);
        }
        cursor.close();
        db.close();
        return dtosss;
    }
}
